package henu.innovation.controller;

import henu.innovation.entity.Admin;
import henu.innovation.entity.Teacher;
import henu.innovation.service.AdminService;
import henu.innovation.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private TeacherService teacherService;

    //代表当前登录 管理员 ，-1：未登录  0：默认admin 其他：数据库中管理员id

    /***
     * 无条件查询所有管理员
     * @param mv
     * @param request
     * @return demo1
     */
    @RequestMapping("/findAllAdmins")
    public ModelAndView findAllAdmins(ModelAndView mv, HttpServletRequest request){
        List<Admin> admins = adminService.findAllAdmins();
        request.setAttribute("admins",admins);
        mv.setViewName("index");
        return mv;
    }

    /***
     * 管理员登录
     * @param name 用户名
     * @param pass 密码
     * @param session
     * @return 管理员首页或登录页面admin_main/index
     */
    @RequestMapping("/admin_login")
    public ModelAndView login(String name, String pass, HttpSession session){
        Admin admin = adminService.login(name,pass);
        ModelAndView mv = new ModelAndView();
        if(admin != null){
            session.setAttribute("adminid",admin.getId());
            session.setAttribute("adminname",admin.getUsername());
            mv.setViewName("Admin/admin_main");
        }else{
            mv.addObject("msg","用户名或口令错误");
            mv.setViewName("redirect:/index.jsp");
        }
        return mv;
    }

    /***
     * 管理员退出
     * @param session
     * @return index
     */
    @RequestMapping("/admin_logout")
    public ModelAndView logout(HttpSession session){
        ModelAndView mv = new ModelAndView();
        session.removeAttribute("adminid");
        session.removeAttribute("adminname");
        mv.setViewName("redirect:/index.jsp");
        return mv;
    }

    /***
     * 管理员修改密码
     * @param mv
     * @param oldpass 老密码
     * @param newpass1 新密码
     * @param newpass2 再输入一次
     * @return 首页admin_main
     */
    @RequestMapping("/change_pass")
    public ModelAndView changePass(ModelAndView mv,String oldpass,String newpass1,String newpass2,HttpSession session){
//        如果当前id不为-1、0，则进入修改，否则返回管理员主页面
        int id = (int)session.getAttribute("adminid");
        if(id != -1 && id != 0){
            Admin admin = adminService.findAdminById(id);
//            原密码输入正确
            if(oldpass.equals(admin.getPassword())){
//                两次密码输入一致
                if(newpass1.equals(newpass2)){
                    admin.setPassword(newpass1);
                    if(adminService.updateAdmin(admin)){
                        mv.addObject("msg","口令修改成功，请重新登录！");
//                        调用退出方法
                        return logout(session);
                    }else{
                        mv.addObject("msg","未知错误，未修改成功！");
                    }
                }else{
                    mv.addObject("msg","两次输入不一致！");
                }
            }else{
                mv.addObject("msg","原口令输入错误！");
            }
        }
        mv.setViewName("Admin/admin_main");
        return mv;
    }

    @RequestMapping("admin_teacher_edit")
    public ModelAndView updateInfo(int id,String name,String pass,String fullname,String isadmin){
        boolean teacherAdmin = isadmin.equals("true,");
//        System.out.println(isadmin);
        Teacher teacher = teacherService.FindTeacherById(id);
//        System.out.println("原teacher："+teacher);
        if(pass != null && pass.length() != 0){
            teacher.setPassword(pass);
        }
        //本就是管理员
        if(teacher.isIsadmin()){
            //要通过原name找到admin
            Admin admin = adminService.findAdminByName(teacher.getName());
            //后来也是
            if(teacherAdmin) {
                admin.setUsername(name);
                admin.setPassword(teacher.getPassword());
                adminService.updateAdmin(admin);
                System.out.println("ben后admin"+admin);
            }
            else{//后来不是
                adminService.deleteAdminById(admin.getId());
            }
        }else{//本不是管理员
            //后来是
            if(teacherAdmin){
                Admin admin = new Admin(name,teacher.getPassword());
                adminService.addAdmin(admin);
                System.out.println("后admin"+admin);
            }
        }
        teacher.setName(name);
        teacher.setFullname(fullname);
        teacher.setIsadmin(teacherAdmin);

//        System.out.println("后teacher："+teacher);
        teacherService.updateTeacher(teacher);
        return adminTeacher();
    }

    /**
     * 添加教师
     * @param name name
     * @param pass 密码
     * @param fullname 全名
     * @param isadmin 是否管理员
     * @return admin_teacher
     */
    @PostMapping("addTeacher")
    public ModelAndView addTeacher(String name,String pass,String fullname,String isadmin){
        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setPassword(pass);
        teacher.setFullname(fullname);

//        如果同时成为管理员
        if(isadmin.equals("true,")){
            teacher.setIsadmin(true);
//            在管理员表中添加数据
            adminService.addAdmin(new Admin(name,pass));
        }
//        在教师表中添加
        teacherService.addTeacher(teacher);
        return adminTeacher();
    }

    @PostMapping("deleteTeacher")
    public ModelAndView deleteTeacher(int id,HttpSession session){
        System.out.println(id);
        Teacher teacher = teacherService.FindTeacherById(id);
//        先从教师表中删除
        teacherService.deleteTeacherById(id);
        if(teacher.isIsadmin()){
//            通过name得到相对应的管理员表中的信息
            Admin admin = adminService.findAdminByName(teacher.getName());
//            然后删除
            adminService.deleteAdminById(admin.getId());
        }
//        返回此时查询到的所有教师信息
        ModelAndView mv = new ModelAndView("Admin/admin_teacher");
        mv.addObject("teachers",teacherService.findAllTeachers());
        return mv;
    }

    /**
     * 想通过ajax传值，但是显示方面有问题，暂时不用
     * @param id
     * @return
     */
    @PostMapping("deleteTeacherAjax")
    @ResponseBody
    public Map<String,Object> deleteTeacherAjax(int id){
        System.out.println(id);
        Teacher teacher = teacherService.FindTeacherById(id);
//        先从教师表中删除
        if(teacherService.deleteTeacherById(id)){
//            通过教师的用户名、密码得到相对应的管理员表中的信息
            Admin admin = adminService.findAdminByName(teacher.getName());
//            然后删除
            if(admin != null){
                adminService.deleteAdminById(admin.getId());
            }
        }
//        返回此时查询到的所有教师信息
        return getAllTeachers();
    }

    public Map<String,Object> getAllTeachers(){
        List<Teacher> list = teacherService.findAllTeachers();
        Map<String,Object> map = new HashMap<>();
        map.put("teachers",list);
        return map;
    }

    /**
     * 跳转管理员主页面
     * @return admin_main
     */
    @RequestMapping("/admin_main")
    public String adminMain(){
        return "Admin/admin_main";
    }

    /**
     * 跳转教师管理页面
     * @return admin_teacher
     */
    @RequestMapping("/admin_teacher")
    public ModelAndView adminTeacher(){
        ModelAndView mv = new ModelAndView("Admin/admin_teacher");
        mv.addObject("teachers",teacherService.findAllTeachers());
        return mv;
    }

    /**
     * 跳转考试管理页面
     * @return admin_exam
     */
    @RequestMapping("/admin_exam")
    public ModelAndView adminExam(){
        ModelAndView mv = new ModelAndView("Admin/admin_exam");
        mv.addObject("exams",teacherService.showAllExams());
        return mv;
    }

    /**
     * 跳转系统配置页面
     * @return admin_config
     */
    @RequestMapping("/admin_config")
    public String adminConfig(){
        return "Admin/admin_config";
    }

    /**
     * 跳转教师编辑页面
     * @param id 教师id
     * @return admin_teacher_modify
     */
    @RequestMapping("/admin_teacher_modify")
    public ModelAndView adminTeacherModify(int id){
        Teacher teacher =teacherService.FindTeacherById(id);
        ModelAndView mv = new ModelAndView("Admin/admin_teacher_modify");
        mv.addObject("id",id);
        mv.addObject("name",teacher.getName());
        mv.addObject("fullname",teacher.getFullname());
        mv.addObject("isadmin",teacher.isIsadmin());
        return mv;
    }
}
